﻿using Gufa.WebAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;

namespace Gufa.WebAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ResumesController : ControllerBase
    {
        private readonly IWebHostEnvironment _hostingEnvironment;

        public ResumesController(IWebHostEnvironment webHostEnvironment)
        {
            this._hostingEnvironment = webHostEnvironment;
        }
        // GET api/resumes
        [HttpGet]
        public IActionResult DownloadResume()
        {
            string _resumeFilePath = Path.Combine(_hostingEnvironment.WebRootPath,"resume","刘楚轩的简历.pdf"); // 替换为实际的简历文件路径
            try
            {
                // 检查简历文件是否存在
                if (!System.IO.File.Exists(_resumeFilePath))
                {
                    return NotFound("Resume file not found");
                }

                // 读取简历文件内容
                byte[] fileBytes = System.IO.File.ReadAllBytes(_resumeFilePath);

                // 设置响应内容为二进制流，并设置响应头
                return new FileContentResult(fileBytes, "application/pdf")
                {
                    FileDownloadName = "resume.pdf",
                };
            }
            catch (Exception ex)
            {
                // 处理异常，返回适当的错误响应
                return StatusCode(500, $"Internal server error: {ex.Message}");
            }
        }
    }
}
